/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.codeview.CodeDemo;
import com.cv4j.codeview.CodeView;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.filters.CompositeFilters;
import com.cv4j.core.filters.NatureFilter;
import com.cv4j.core.filters.SpotlightFilter;

/**
 * 组合控件
 *
 * @since 2021-05-18
 */
public class CompositeFilersAbilitySlice extends BaseAbilitySlice {
    private Image image1;
    private Image image2;
    private Image image3;

    /**
     * 初始化
     *
     * @return int
     */
    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_composite_filters;
    }

    /**
     * 布局初始化
     *
     * @param intent intent
     */
    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    /**
     * 布局初始化
     */
    private void initView() {
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image3 = (Image) findComponentById(ResourceTable.Id_image3);
        PixelMap pixelMap = PixelMapUtil.getPixelMapFromResource(getContext(),
            ResourceTable.Media_test_filters).get();
        CV4JImage cv4Jimage = new CV4JImage(pixelMap);
        image1.setPixelMap(new NatureFilter().filter(cv4Jimage.getProcessor()).get().getImage().toBitmap());
        CV4JImage cv4Jimage1 = new CV4JImage(pixelMap);
        image2.setPixelMap(new SpotlightFilter().filter(cv4Jimage1.getProcessor()).get().getImage().toBitmap());
        CV4JImage cv4Jimage3 = new CV4JImage(pixelMap);
        CompositeFilters compositeFilters = new CompositeFilters();
        PixelMap pixelMap3 = compositeFilters.addFilter(new NatureFilter())
            .addFilter(new SpotlightFilter()).filter(cv4Jimage3.getProcessor()).get().getImage().toBitmap();
        image3.setPixelMap(pixelMap3);

        CodeView codeview3 = (CodeView) findComponentById(ResourceTable.Id_codeview3);
        codeview3.showCode(CodeDemo.getDemo3());
    }
}
